import time
import threading
import schedule


def job1():
    print("job1")


def job2(id):
    print(f"job2 {id}")


# 添加任务
def add_job(func, interval, param=None):
    if func == job1:
        schedule.every(interval).seconds.do(func)
    else:
        schedule.every(interval).seconds.do(func, param)


def del_job(func):
    for job in schedule.jobs:
        # 通过函数名 删除任务
        if job.job_func.func == func:
            schedule.cancel_job(job)
            break


def main():
    while True:
        schedule.run_pending()
        time.sleep(1)


if __name__ == "__main__":
    threading.Thread(target=main, daemon=True).start()
    help_str = """
1 添加任务job1 -- a 删除任务job1
2 添加任务job2 -- b 删除任务job2
"""
    while True:
        print(help_str)
        cmd = input("请输入命令：")
        if cmd == "1":
            add_job(job1, 2)
        elif cmd == "2":
            add_job(job2, 3, "test")
        elif cmd == "a":
            del_job(job1)
        elif cmd == "b":
            del_job(job2)
